package com.ywgt;

/**
 * 字符串暴力匹配
 */
public class ViolenceMatch {
    public static void main(String[] args) {
        String str1="阿打算放大发斯蒂芬斯蒂上发顺丰奥术大师大防守打法是发斯蒂芬斯蒂芬发送千万人";
        String str2="大师大防守打";
        int index = getIndex(str1, str2);
        int indexOf = str1.indexOf("大师大防守打");
        System.out.println(indexOf);
        System.out.println(index);
    }

    public static int getIndex(String str1,String str2){
        char[] chars1 = str1.toCharArray();
        char[] chars2 = str2.toCharArray();
        int i=0;
        int j=0;

        while(i<str1.length()&&j<str2.length()){
            if(chars1[i]==chars2[j]){
                i++;
                j++;
            }else {
                i = i - (j - 1);
                j = 0;
            }
        }
        if(j==str2.length()) {
            return i - j;
        }
        return -1;
    }
}
